package com.wsoft.form.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.entity.WfFormVirtualColumnEntity;
import com.wsoft.form.mapper.WfFormVirtualColumnMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/30 09:30
 */
@Service
public class WfFormVirtualColumnManager extends ServiceImpl<WfFormVirtualColumnMapper, WfFormVirtualColumnEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 查询tableID 下所有虚拟字段
     *
     * @param tableId 表id
     * @return list
     */
    @GetRedisCache(type = RedisKeyConstant.Form.VIRTUAL_COLUMN_LIST,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<WfFormVirtualColumnEntity> getByTableId(Long tableId) {
        QueryWrapper<WfFormVirtualColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormVirtualColumnEntity::getTableId, tableId);
        return list(query);
    }

    /**
     * 查询 aggregationTableId 下的虚拟字段
     *
     * @param aggregationTableId
     * @return
     */
    public List<WfFormVirtualColumnEntity> getByAggregationTableId(Long formId, Long aggregationTableId) {
        QueryWrapper<WfFormVirtualColumnEntity> query = new QueryWrapper<>();
        query.lambda()
                .eq(WfFormVirtualColumnEntity::getFormId, formId)
                .eq(WfFormVirtualColumnEntity::getAggregationTableId, aggregationTableId);
        return list(query);
    }

    /**
     * 查询tableID 下所有虚拟字段
     *
     * @param tableId 表id
     * @return list
     */
    public List<WfFormVirtualColumnEntity> getByTableId(List<Long> tableIdList) {
        List<WfFormVirtualColumnEntity> list = new ArrayList<>();
        for (Long tableId : tableIdList) {
            list.addAll(this.getByTableId(tableId));
        }
        return list;
    }

    /**
     * 根据 ColumnName 查询
     *
     * @param tableId    tableId
     * @param columnName 列名称
     * @return entity
     */
    public WfFormVirtualColumnEntity getByColumnName(Long tableId, String columnName) {
        QueryWrapper<WfFormVirtualColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormVirtualColumnEntity::getTableId, tableId)
                .eq(WfFormVirtualColumnEntity::getColumnName, columnName);
        return getOne(query);
    }

    /**
     * 根据 formId 删除
     *
     * @param formId formId
     * @return boolean
     */
    public boolean removeByFormId(Long formId) {
        QueryWrapper<WfFormVirtualColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormVirtualColumnEntity::getFormId, formId);
        boolean b = remove(query);
        if (b) {
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.VIRTUAL_COLUMN_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFormVirtualColumnEntity entity) {
        boolean b = super.save(entity);
        if (b) {
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.VIRTUAL_COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id跟新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFormVirtualColumnEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.VIRTUAL_COLUMN_INFO + entity.getId());
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.VIRTUAL_COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public boolean removeById(Long id) {
        boolean b = super.removeById(id);
        if (b) {
            redisService.del(RedisKeyConstant.Form.VIRTUAL_COLUMN_INFO + id);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.VIRTUAL_COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }


    /**
     * 根据聚合字段 绑定的字段id查询
     * @param aggregationColumnId
     * @return
     */
    public List<WfFormVirtualColumnEntity> getByAggregationColumn(Long aggregationColumnId){
        QueryWrapper<WfFormVirtualColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormVirtualColumnEntity::getAggregationColumnId, aggregationColumnId);
        return list(query);
    }


    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.VIRTUAL_COLUMN_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormVirtualColumnEntity getById(Long id) {
        return super.getById(id);
    }
}
